import { FOOTMARK } from '../config.js'

const state = {
	footmark: uni.getStorageSync(FOOTMARK) || [] //记录列表
}

const getters = {
	get (state) {
		return state.footmark
	}
}

const mutations = {
	set (state, footmark) {
		state.footmark = footmark
		uni.setStorageSync(FOOTMARK, state.footmark)
	}
}

const actions = {
	save ({state, commit}, footmark) {
		const list = [...state.footmark]
		const index = list.findIndex(item => item.id == footmark.id)
		if ( index > -1 ) {
			list.splice(index, 1)
			list.unshift(footmark)
		} else {
			list.unshift(footmark)
		}
		commit('set', list)
	},
	remove ({state, commit}, id) {
		const list = [...state.footmark]
		const index = list.findIndex(footmark => footmark.id == id)
		if ( index > -1 ) list.splice(index, 1);
		commit('set', list)
	},
	clear ({commit}) {
		commit('set', [])
	}
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
	actions
}